package com.bw.usercenter.ui

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationManager
import android.database.sqlite.SQLiteDatabase
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.bw.common.sp.SPPropDelegate
import com.bw.mvpcore.view.BaseMVPActivity
import com.bw.usercenter.R
import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.resp.RespRecommendEntity
import com.bw.usercenter.model.protocol.resp.RespSearchEntity
import com.bw.usercenter.model.protocol.resp.RespSearchitemIDEntity
import com.bw.usercenter.presenter.UserCenterPresenterImpl
import com.bw.usercenter.sq.MySQ
import com.bw.usercenter.sq.MySQHelper
import kotlinx.android.synthetic.main.activity_log_pass.*
import org.jetbrains.anko.toast
import java.util.*

class LogActivityPass: BaseMVPActivity<UserCenterPresenterImpl>(), UserCenterContract.UserCenterView  {

    var num:Int = 20
    var istimer:Boolean = true
    var timer:Timer?=null
    var numcode:Int ?=null
    private var phone:String by SPPropDelegate("phone","")
    var handler:Handler = object :Handler(){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what==100){
                getcode.setText("$num 秒")
                num--
                if (num==1){
                    num = 20
                    getcode.isFocusable=false
                    getcode.isFocusableInTouchMode = false
                    getcode.requestFocus()
                    getcode.setText("重新获取")
                    timer?.cancel()
                }
            }
        }
    }
    override fun getLayoutId(): Int {
        return R.layout.activity_log_pass
    }

    override fun ClearStatBar() {

    }

    @SuppressLint("WrongConstant", "NewApi")
    @RequiresApi(Build.VERSION_CODES.O)
    override fun initEvent() {


        //获取验证码
        getcode.setOnClickListener {
            timer = Timer()
            if (num==20){
                mPresenter.getauthcode()
                getcode.isFocusable = true
                getcode.isFocusableInTouchMode = true
                getcode.requestFocus()
                timer?.schedule(object : TimerTask() {
                    override fun run() {
                        handler.sendEmptyMessage(100)
                    }
                },0,500)
            }else{
                toast("获取失败!")
            }
        }
        regpbtnok.setOnClickListener {
            var username = regp_username.text.toString().trim()
            var nucode = regp_numcode.text.toString().trim()
            if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(nucode)){
                toast("手机号或验证码不能为空")
            }else{
                var rep_user = "^[1]{1}[3-9]{1}[0-9]{9}$"
                if (!username.matches(Regex(rep_user))){
                    toast("手机号格式有误!")
                }else{
                    if (nucode.equals(numcode.toString())){
                        toast("登录成功")
                        phone = username
                        jumpActivity(MainActivity::class.java)
                    }else{
                        toast("验证码错误")

                    }
                }


            }
        }
        //密码登录
        passcodep.setOnClickListener {
            jumpActivity(LogActivity::class.java)
        }
        //z注册
        reguserp.setOnClickListener {
            jumpActivity(RegActivity::class.java)
        }

    }

    override fun initData(savedInstanceState: Bundle?) {

    }


    override fun releaseResource() {

    }

    override fun createPresenter(): UserCenterPresenterImpl {
        return UserCenterPresenterImpl(this@LogActivityPass)
    }

    override fun <T> registerSuccess(data: T) {

        numcode = data as Int

        val notification:Notification.Builder = Notification.Builder(this@LogActivityPass)
            notification.setSmallIcon(R.drawable.ic_launcher_background)
            notification.setContentText("获取到的验证码为:$numcode")
            val notificationManager:NotificationManager = application.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.notify(1,notification.build())

    }

    override fun registerSuccess(data: RespRecommendEntity) {

    }

    override fun registerFaild(throwable: Throwable) {

    }

    override fun registerSrech(respSearchEntity: RespSearchEntity) {

    }

    override fun getSearchitem(respSearchitemIDEntity: RespSearchitemIDEntity) {

    }
}